#include<iostream>

using namespace std;
const int N = 1e5 + 10;
using ll = long long;
ll a[N];
int main()
{
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    ll ret = 0;
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == a[i - 1]) continue;
        else if (a[i - 1] < a[i]) ret += abs(a[i - 1] - a[i]);
    }

    cout << ret << endl;
    return 0;
}